package com.datacow.util;

import com.alibaba.fastjson.JSONObject;
import com.datacow.modules.climbingdata.constant.ProductHuntInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 匹配过滤的工具类
 * @Author: 李德森
 * @Date: 2021/1/15
 */


public class MatchFilterUtil {
    /**
     * @Description: 匹配过滤表情
     * @Param: content
     */
    public static String removeFourChar(String content) {
        byte[] conByte = content.getBytes();
        for (int i = 0; i < conByte.length; i++) {
            if ((conByte[i] & 0xF8) == 0xF0) {
                for (int j = 0; j < 4; j++) {
                    conByte[i + j] = 0x30;
                }
                i += 3;
            }
        }
        content = new String(conByte);
        return content.replaceAll("0000", "");
    }

    /**
     * @Description: 匹配过滤emoji表情
     * @Param: content
     */
    public static String removeEmoji(String content) {
        String patternRule = ProductHuntInfo.MatchingRule.FILTER_EMOJI;
        Pattern pattern = Pattern.compile(patternRule);
        Matcher matcher = pattern.matcher(content);
        return matcher.replaceAll("");
    }

    /**
     * 过滤掉所有标签取出指定内容并封装成json格式
     *
     * @param patternRule 匹配规则
     * @param textContent 被匹配内容
     * @return String
     */
    public static String getDescriptionEnJson(String patternRule, String textContent) {
        String descriptionEnMapToJson = null;
        //Map<Integer, String> descriptionEnMap = new HashMap<>();
        StringBuffer stringBuffer = new StringBuffer();
        //把所有标签替换成空
        String replaceEmpty = textContent.replaceAll(patternRule, "");
        //把所有空行删除,用换行符分割
        String deleteEmptyLine = replaceEmpty.replaceAll("((\r\n)|\n)[\\s\t ]*+", "\n");
        //删除表情符号
        String deleteEmoji = deleteEmptyLine.replaceAll("[^A-Za-z0-9,.()?]\\s", "")
                .replaceAll("[^\\sA-Za-z0-9,.()?]", "")
                .trim();
        //开始以换行符分割文本内容
        String[] ContentSplit = deleteEmoji.split("\n");
        for (int i = 0; i < ContentSplit.length; i++) {
           //descriptionEnMap.put(i + 1, i + 1 + "、" + ContentSplit[i]);
           stringBuffer.append(ContentSplit[i]);
        }
        /*//把存入的Map类型数据转为Json格式
        descriptionEnMapToJson = JSONObject.toJSON(stringBuffer).toString();*/

        return stringBuffer.toString();
    }
}
